/*
 * CS_SOCIAL_SEND_STAMINA_SM.cpp
 *
 *  Created on: 2014. 4. 17.
 *      Author: mega
 */

#include "../../User.h"
#include "../../Command.h"
#include "../../Proxy.h"
#include "../../NLogic/DailyMission.h"
#include "../../NResource/Static.h"
#include "../../NProcess/Static.h"

namespace ProjectZ { namespace NCommand {

void SendPushStamina(User * pUser, int receiveUserSeq)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT company, sale_cd, uuid FROM info_user WHERE user_seq = %d", receiveUserSeq);
	if (false == ::Database::GETSESSION()->SyncExecute(static_cast<int>(DatabaseType::GAME_SLAVE), clsAuto))
		return;

	if( clsAuto->GetErrorIdx() != 0 )
	{
		DEBUGLOG("%s clsAuto->GetErrorIdx() != 0", __FUNCTION__ );
		return;
	}

	if (0 == clsAuto->ResultRowCount())
	{
		DEBUGLOG("%s 0 == clsAuto->ResultRowCount()", __FUNCTION__ );
		return;
	}

	int company = 0;
	int sale_cd = 0;
	::Toolkit::Str<81> uuid;

	while( clsAuto->Next() )
	{
		clsAuto->GetRecord() >> company >> sale_cd >> uuid;
	}

	{

		NProcess::PushSender::Param * clsParam = NProcess::Static::Instance().GetParamPool()->SAFE_NEW();

		if (company == 5)
			clsParam->_eType = NProcess::PushSender::Param::Type::ANDROID;
		else
			clsParam->_eType = NProcess::PushSender::Param::Type::IOS;

		NResource::MessageResource * pMessageResource = NResource::Static::InstancePtr()->GetMessageResource();
		if (NULL == pMessageResource)
			return;

		std::string sender;
		std::string message;

		if (false == pMessageResource->SetMessageFromType(company, sale_cd, NResource::MessageData::message_type::send_heart, sender, message))
			return;

		Toolkit::Str<400> sendMessage;
		sendMessage.Format(message.c_str(), pUser->GetUserInfo().nickname.c_str());

		clsParam->_sender	= sender.c_str();
		clsParam->_message	= sendMessage.c_str();
		clsParam->_uuid		= uuid.c_str();

		NProcess::Static::Instance().GetPushSender().Push(clsParam);
	}
}

void Command::CS_SOCIAL_SEND_STAMINA_SM(User * pUser, ::Network::Packet * pPacket)
{
/*
	// 친구에게 스태미나 보내기
	CS_SOCIAL_SEND_STAMINA_SM    0x2168
	{
		u32 u32gid;               // 스태미나와 우정포인트 받을 상대의 GID
		u16 u16is_push_msg;    // 푸쉬 메시지 유형 (0 : 카톡 메시지(클라이언트), 1: 서버 푸시)
	}

	SC_SOCIAL_SEND_STAMINA_SM    0x2169
	{
		s16 s16ack;
		u32 u32period;           // 누적 버프 (초시간) : 최대 60분 (60 X 60 = 3600 초)
	}
*/

	PREPARE_PACKET;

	SendStaminaSyn msg;

	*pPacket >> msg.gid >> msg.is_push;

	if (0 >= msg.gid)
	{
		::Network::Packet *pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_UNKNOWN_ERROR);
		pAck->MakePacket(CMD_SC_SOCIAL_SEND_STAMINA_SM);
		pUser->Send(pAck);
		return;
	}

	msg.send_heart_count = SEND_HEART_COUNT;
	msg.send_stamina_count = SEND_STAMINA_COUNT;

	::ProjectZ::NProxy::SendFriendsServer(pUser->GetUserSeq(), msg);

	if (1 == msg.is_push)
	{
		SendPushStamina(pUser, msg.gid);
	}

	// 일일 미션
	NLogic::DailyMission * pDailyMission = pUser->GetDailyMission();
	if (NULL != pDailyMission)
	{
		pDailyMission->MissionComplete(NLogic::DailyMission::MissionType::MISSION7);
	}
}

} /*NCommand*/ } /*ProjectZ*/
